package cn.edu.neu.springbootdemo.action;

import cn.edu.neu.springbootdemo.core.Constants;
import cn.edu.neu.springbootdemo.model.*;
import cn.edu.neu.springbootdemo.service.MovieChartService;
import cn.edu.neu.springbootdemo.service.MovieService;
import cn.edu.neu.springbootdemo.service.Orderservice;
import cn.edu.neu.springbootdemo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserAction {
    @Resource(name = "userServiceImpl")
    UserService userService;

    @Resource(name = "movieChartServiceImpl")
    private MovieChartService movieChartService;

    @Resource(name = "movieServiceImpl")
    private MovieService movieService;

    @Resource(name = "orderServicelmpl")
    private Orderservice orderService;

    @RequestMapping("/buy")
    public String userbuy(MovieChart movieChart, HttpSession session, Model model) {
        User user = (User) session.getAttribute(Constants.LOGIN_USER);
        MovieChart movieChart1 = movieChartService.findById(movieChart.getShow_id());
        String[] Seat=orderService.Seat(movieChart.getShow_id());
        model.addAttribute("Seat",Seat);
        model.addAttribute("MovieList", movieChart1);
        return "/user/buy";
    }

    @RequestMapping("/home")
    public String userbuy(Model model){
        List<Movie> list=movieService.showMovieList();
        model.addAttribute("MovieList",list);
        return "/user/Home";
    }
    @RequestMapping("/personal")
    public String personal(HttpSession session,Model model){
        User user = (User) session.getAttribute(Constants.LOGIN_USER);
        User user1= userService.findById(user);
        model.addAttribute("userList",user1);
        System.out.println(user1+"++++++++++++++++++++++++++user++++++++++++++++++++++++++++++++++");
        return "/user/showPersonal";
    }
    @RequestMapping("/UpdataUser")
    public  String UpdataUser(User user){
        System.out.println(user+"+++++++++++++++11561321");
        userService.upData(user);
        return "/admin/result";
    }

    @RequestMapping("/details")
    public String details(int id,Model model){
        Movie  movie=  movieService.findById(id);
        List<MovieChart>  movie1= movieChartService.findfilm_id(id);
        System.out.println(movie1+"+++++++++++++++movie1++++++++++");
        model.addAttribute("movie",movie);
        model.addAttribute("movieList",movie1);
        return "/user/film_details";
    }
    @RequestMapping("/buyickets")
    public   String buyickets(HttpSession session,Order order){

        String[] Seats=order.getSeat().split(",");
         System.out.println(order.getShow_id()+"sum:"+order.getSum()+"seat:"+order.getSeat()+"+++++++++++++++++++++++++++oreder+++++++++++++++++++++++++");
         User user = (User) session.getAttribute(Constants.LOGIN_USER);
         order.setUser_id(user.getUser_id());
         synchronized (this){
             for(int i=0;i<Integer.parseInt(order.getSum());i++){
                order.setSeat(Seats[i]);
               orderService.addMOrder(order);
             }
         }

        return "/admin/result";
    }

    @RequestMapping("ping")
    public String getrate(int id,Model model){
        Pingfen a = userService.getRate(id);
        model.addAttribute("a",a);
        return "/user/pingfen";
    }
    @RequestMapping("addrate")
    @ResponseBody
    public void addRate(int id,int rate){
        userService.addRate(id, rate);
    }

    //显示电影列表
    @RequestMapping("/showMovie")
    public String showMovie(Model model){

        List<Movie> list=movieService.showMovieList();
        model.addAttribute("MovieList",list);
        return "/user/movie";
    }


}
